package mysql

import (
	"video/videorpc/videorpc"

	"gorm.io/gorm"
)

type Video struct {
	gorm.Model
	VideoTitle    string `gorm:"type:varchar(20);not null"`
	VideoCategory string `gorm:"type:char(32);not null"`
	VideoLink     string `gorm:"type:varchar(500);not null"`      //视频
	CoverLink     string `gorm:"type:varchar(500);not null"`      //封面
	VideoIntro    string `gorm:"type:varchar(500)"`               //简介
	PlayNum       int    `gorm:"type:int(11);default:0;not null"` //播放量
	LikeNum       int    `gorm:"type:int(11);default:0;not null"`
	CollectNum    int    `gorm:"type:int(11);default:0;not null"`
	CommentNum    int    `gorm:"type:int(11);default:0;not null"`
	ReleaseTime   int    `gorm:"type:int(11);default:0;not null"`
	TopicId       int    `gorm:"type:int(11);not null"`
	UserId        int    `gorm:"type:int(11);not null"`
}

func (v *Video) VideoAdd(db *gorm.DB) error {
	return db.Debug().Create(v).Error
}

func (v *Video) UpdateVideo(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Updates(v).Error
}

func (v *Video) VideoDel(db *gorm.DB, VideoId int64, UserId int64) error {
	return db.Debug().Where("id=?", VideoId).Where("user_id=?", UserId).Delete(v).Error
}

func VideoByTopic(db *gorm.DB, in *videorpc.VideoShowListRequest) []*videorpc.VideoShowList {
	var list []*videorpc.VideoShowList
	db.Debug().Model(&Video{}).
		Select("videos.id",
			"videos.created_at",
			"videos.video_title",
			"videos.video_category",
			"videos.video_link",
			"videos.play_num",
			"topics.topic_name",
			"topics.topic_intro",
		).
		Joins("LEFT JOIN topics ON videos.topic_id=topics.id").
		Find(&list)
	return list
}

func VideoList(db *gorm.DB, in *videorpc.VideoListRequest) []*videorpc.VideoList {

	var list []*videorpc.VideoList
	db.Debug().Model(&Video{}).
		Select("videos.id",
			"videos.created_at",
			"videos.video_title",
			"videos.video_category",
			"videos.video_link",
			"videos.play_num",
			" users.nick_name").
		Joins("LEFT JOIN users ON videos.user_id=users.id").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Order("videos.play_num desc").
		Find(&list)
	return list
}

func VideoShow(db *gorm.DB, in *videorpc.VideoShowRequest) []*videorpc.VideoShow {

	var show []*videorpc.VideoShow
	db.Debug().Model(&Video{}).
		Select("videos.id",
			"videos.created_at",
			"videos.video_title",
			"videos.video_category",
			"videos.video_link",
			"videos.play_num",
			" users.nick_name").
		Joins("LEFT JOIN users ON videos.user_id=users.id").
		Where("videos.play_num=?", in.PlayNum).
		Find(&show)
	return show
}
